﻿#region Using
using nj4x;
using nj4x.Metatrader;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
#endregion

namespace MT4
{
    public partial class Form1 : Form
    {

        List<Account> accounts = FileManager.FileManager.Deserialize<List<Account>>("accounts");

        public Form1()
        {
            InitializeComponent();
            
            foreach (Account a in accounts)
            {
                a.Connect();
                ListViewItem broker = new ListViewItem(a.Broker);
                broker.SubItems.Add(a.Login);
                broker.SubItems.Add(a.Strategy().AccountBalance().ToString());
                broker.SubItems.Add(a.Strategy().AccountEquity().ToString());
                broker.SubItems.Add(a.Strategy().AccountCurrency());
                broker.Tag = a;
                accountsListView.Items.Add(broker);
                foreach (var trade in a.getOpenTrades())
                {
                    ListViewItem tradeItem = new ListViewItem(a.Broker);
                    tradeItem.SubItems.Add(a.Login);
                    tradeItem.SubItems.Add(trade.GetTicket().ToString());
                    tradeItem.SubItems.Add(trade.GetSymbol().ToString());
                    tradeItem.SubItems.Add(trade.GetTradeOperation().ToString());
                    tradeItem.SubItems.Add(trade.GetTakeProfit().ToString());
                    tradeItem.SubItems.Add(trade.GetStopLoss().ToString());
                    tradeItem.SubItems.Add(trade.GetOpenPrice().ToString());
                    tradeItem.SubItems.Add(trade.GetOpenTime().ToString());
                    tradeItem.SubItems.Add(trade.GetProfit().ToString());
                    tradeItem.SubItems.Add(a.Strategy().AccountCurrency());
                    tradeItem.SubItems.Add(trade.GetMagic().ToString());

                    tradeItem.Tag = a;

                    openTradesListView.Items.Add(tradeItem);
                }

            }

            equityRefresher.Enabled = true;
            tradesRefresher.Enabled = true;
        }





        private void generateTestAccounts()
        {
            List<Account> accounts = new List<Account>();

            accounts.Add(new Account("22096041", "7anwzlx", "GO4X-Demo"));
            accounts.Add(new Account("100269", "0lfijdm", "NobleSecurities-Demo"));

            FileManager.FileManager.Serialize<List<Account>>("accounts", accounts);
        }

        private void equityRefresher_Tick(object sender, EventArgs e)
        {
            foreach (ListViewItem item in accountsListView.Items)
            {
                if (item.Tag is Account)
                {
                    Account acc = (Account)item.Tag;
                    item.SubItems[2].Text = acc.Strategy().AccountBalance().ToString();
                    item.SubItems[3].Text = acc.Strategy().AccountEquity().ToString();
                }
            }
        }

        DateTime lastRefresh = DateTime.Now;

        private void tradesRefresher_Tick(object sender, EventArgs e)
        {
            List<ListViewItem> toRemove = new List<ListViewItem>();

            foreach (ListViewItem item in openTradesListView.Items)
            {
                if (item.Tag is Account)
                {
                    Account account = (Account)item.Tag;

                    // Pobieramy zlecenie na nowo
                    IOrderInfo order = account.Strategy().OrderGet(Int32.Parse(item.SubItems[2].Text), SelectionType.SELECT_BY_TICKET, SelectionPool.MODE_HISTORY);

                    // Sprawdzamy czy usuniete (czy czas zamknięcia różny od 1970-01-01)
                    if (order.GetCloseTime().CompareTo(new DateTime(1970, 1, 1)) > 0)
                    {
                        // Dodajemy do usunięcia
                        toRemove.Add(item);
                    }
                    else
                    {
                        /**
                         * Zmiany w:
                         * 5 - Take Profit
                         * 6 - Stop Loss
                         * 9 - Get Profit                         * 
                         */
                        item.SubItems[5].Text = order.GetTakeProfit().ToString();
                        item.SubItems[6].Text = order.GetStopLoss().ToString();
                        item.SubItems[9].Text = order.GetProfit().ToString();
                    }
                }
            }

            // Usuwanie zamkniętych zleceń
            foreach (ListViewItem item in toRemove)
                openTradesListView.Items.Remove(item);
        }


        private void addAccountButton_Click(object sender, EventArgs e)
        {
            string acc = loginTextBox.Text;
            string pass = passwordTextBox.Text;

        }

    }
}
